#if UNITY
using UnityEngine;
#endif

namespace NodeBasedSkillEditor.Model.Node.RT
{
    public class Node_Wait_RT : RT_Node
    {
        public RT_FlowPort FlowIn_RT;

        public RT_SingleValuePort Seconds_RT;

        public RT_FlowPort FlowOut_RT;

        public Node_Wait_RT(Node_Wait sourceData, RT_DAG parentDag) : base(sourceData, parentDag)
        {
            FlowIn_RT = new RT_FlowPort(EPortIOType.Input, sourceData.FlowIn, this);

            Seconds_RT = new RT_SingleValuePort(EPortIOType.Input, sourceData.Seconds, this);

            FlowOut_RT = new RT_FlowPort(EPortIOType.Output, sourceData.FlowOut, this);
        }


        public override (EDagExeState, RT_Node) ForewardExecute(DAG_Executor executor)
        {
            float waitTime = Seconds_RT.GetValue(executor).ToScalar().Item2;
            if (waitTime <= 0)
            {
                return TryForewardByPort(FlowOut_RT);
            }
            else
            {
                var waitInfo = new Wait_Info() { WaitTime = waitTime, ElapsedTime = 0 };
                executor.SetExeInfoOfNode(this, waitInfo);
                return (EDagExeState.Update, this);
            }
        }

        public override (EDagExeState, RT_Node) UpdateExecute(DAG_Executor executor)
        {
            var waitInfo = executor.GetExeInfoOfNode(this) as Wait_Info;
            if (waitInfo != null)
            {
#if UNITY
                waitInfo.ElapsedTime += Time.deltaTime;
                if (waitInfo.ElapsedTime > waitInfo.WaitTime)
                {
                    return TryForewardByPort(FlowOut_RT);
                }
                else
                {
                    return (EDagExeState.Update, this);
                }
#endif
            }
            return TryForewardByPort(FlowOut_RT);
        }

    }

    public class Wait_Info : RT_NodeExeInfo
    {
        public float WaitTime;
        public float ElapsedTime;
    }
}